package info.liuxing.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;

/**
 * CustomTypeFilter
 *
 * @author liuxing
 * @summary CustomTypeFilter
 * @Copyright (c) 2018, Lianjia Group All Rights Reserved.
 * @since 2018-04-08 10:14
 */
public class CustomTypeFilter implements TypeFilter {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomTypeFilter.class);

    /**
     * 扫描包后的匹配规则
     * <p>
     * 扫描包下的任何类都会通过这个匹配方法
     *
     * @param metadataReader        读取到的当期那正在扫描的类的信息
     * @param metadataReaderFactory 可以获取到其他任何类的信息
     * @return
     * @throws IOException
     */
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        // 获取当前类注解的信息
        AnnotatedTypeMetadata annotatedTypeMetadata = metadataReader.getAnnotationMetadata();
        // 获取当前正在扫描的类的类信息
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        // 获取当前类资源(类的路径)
        Resource resource = metadataReader.getResource();

        String className = classMetadata.getClassName();

        LOGGER.info("---> {}", className);

        // 用一个简单的字符判断
        if (className.contains("er")) {
            return true;
        }

        return false;
    }
}
